#include "../tcp_server_v2.hpp"

class echo_server {
private:
    tcp_server server;
public:
    echo_server(uint16_t port) : server(port)
    {
        server.enable_inactive_destroy(5);
        server.set_thread_count(2);
        server.set_connected_callback(bind(&echo_server::handle_connected, this, placeholders::_1));
        server.set_messge_callback(bind(&echo_server::handle_msg, this, placeholders::_1, placeholders::_2));
        server.set_closed_callback(bind(&echo_server::handle_close, this, placeholders::_1));
    }

    void start() {
        server.start();
    }
private:
    void handle_connected(const con_ptr &con) {
        // NFO_LOG("client connected, fd = %d, id = %ld", con->get_fd(), con->get_id());
    }

    void handle_messgae(const con_ptr &con, buffer *buf) {
        string echo = "echo# ";
        echo += string(buf->get_read_pos(), buf->get_readable_size());
        buf->move_read_pos(buf->get_readable_size());
        con->send(echo.c_str(), echo.size());
    }

    void handle_close(const con_ptr &con) {
        // INFO_LOG("client disconnected, fd = %d, id = %ld", con->get_fd(), con->get_id());
    }
};